package com.spa.modules.gen.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.spa.common.api.vo.Result;
import com.spa.common.aspect.annotation.AutoLog;
import com.spa.modules.gen.entity.GenData;
import com.spa.modules.gen.service.GenDataService;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 数据源管理
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/gen/data")
@Api(tags = "数据源管理")
public class GenDataController {

	@Autowired
	private GenDataService genDataService;

	/**
	 * 分页列表查询
	 * 
	 * @param spaUser
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
//	@SaCheckPermission("gen:data:listByPage")  
	@AutoLog(value = "数据源-列表")
	@GetMapping(value = "/listByPage")
	public Result<?> queryPageList(HttpServletRequest req,
			@RequestParam(name = "page", defaultValue = "1") Integer page,
			@RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize) {
		return genDataService.queryPageList(req, page, pageSize);
	}

	/**
	 * 数据源下拉接口
	 * 
	 * @param spaUser
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
//	@SaCheckPermission("gen:data:listByPage")  
	@AutoLog(value = "数据源下拉接口")
	@ApiOperation(value = "数据源下拉接口", notes = "数据源下拉接口")
	@GetMapping(value = "/all")
	public Result<?> queryAllDbList() {
		return genDataService.queryAllDbList();
	}

	/**
	 * 根据数据源id获取数据源下的所有表
	 * 
	 * @param dbId
	 * @return
	 */
//	@SaCheckPermission("gen:data:listByPage")  
	@AutoLog(value = "根据数据源id获取数据源下的所有表")
	@ApiOperation(value = "根据数据源id获取数据源下的所有表", notes = "根据数据源id获取数据源下的所有表")
	@GetMapping(value = "/table")
	public Result<?> getTableListByDbId(@RequestParam(name = "dbId", defaultValue = "") String dbId) {
		return genDataService.getTableListByDbId(dbId);
	}

	/**
	 * 添加
	 *
	 * @param spaUser
	 * @return
	 */
//	@SaCheckPermission("gen:data:add")  
	@AutoLog(value = "数据源-添加")
	@ApiOperation(value = "数据源-添加", notes = "数据源-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GenData genData) {
		return genDataService.insert(genData);
	}

	/**
	 * 编辑
	 *
	 * @param spaUser
	 * @return
	 */
//	@SaCheckPermission("gen:data:edit")  
	@AutoLog(value = "数据源-编辑")
	@ApiOperation(value = "数据源-编辑", notes = "数据源-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GenData genData) {
		return genDataService.saveById(genData);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
//	@SaCheckPermission("gen:data:del")  
	@AutoLog(value = "数据源-通过id删除")
	@ApiOperation(value = "数据源-通过id删除", notes = "数据源-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		return genDataService.deleteById(id);
	}

	/**
	 * 测试数据源链接是否有效
	 * 
	 * @param spaUser
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
//	@SaCheckPermission("gen:data:listByPage")  
	@AutoLog(value = "测试数据源链接是否有效")
	@PostMapping(value = "/test")
	public Result<?> testGenDb(@RequestBody GenData genData) {
		return genDataService.testGenDb(genData);
	}

}
